import 'package:card_swiper/card_swiper.dart';
import 'package:flutter/material.dart';
import 'package:flutter_syt/api/api.dart';
import 'package:flutter_syt/datas/article_item.dart';
import 'package:flutter_syt/datas/number_extension.dart';
import 'package:flutter_syt/domain/home/share_api.dart';
import 'package:flutter_syt/domain/news/index_able.dart';
import 'package:flutter_syt/domain/news/news_cover.dart';
import 'package:flutter_syt/domain/news/news_page.dart';
import 'package:flutter_syt/domain/widgets/flow_big_item.dart';
import 'package:flutter_syt/domain/widgets/flow_item.dart';
import 'package:flutter_syt/domain/widgets/scroller_banner.dart';
import 'package:flutter_syt/domain/widgets/stupid_tabs.dart';
import 'package:flutter_syt/utils/click_utils.dart';
import 'package:flutter_syt/widgets/normal_table.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:yyy_route_annotation/yyy_route_annotation.dart';

class NewsListPage extends StatefulWidget with IndexAble {
  final int idx;
  final ScrollerTabEntity entity;

  NewsListPage(this.idx, this.entity);

  @override
  State<StatefulWidget> createState() => NewsListPageState();

  @override
  int get index => this.idx;
}

class NewsListPageState extends State<NewsListPage> {
  NewsCoverEntity? _currentCover;
  final SwiperController _swipeController = SwiperController();
  NormalTableViewController controller = NormalTableViewController();
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    NewsPage.controller[widget.entity.alias] = controller;
  }

  @override
  Widget build(BuildContext context) {
    return NormalTableView<FlowItemEntity>(
      controller: controller,
      itemBuilder: (context, item) {
        return GestureDetector(
          child: item.showBig ? FlowBigItem(item) : FlowItem(item),
          onTap: ClickUtils.debounce(
              () => RouteManager.push(context, item.linkUrl)),
        );
      },
      // refreshItemBuilder:
      //     (BuildContext context, String text, RefreshStatus state) {
      //   if (RefreshStatus.refreshing == state ||
      //       RefreshStatus.canRefresh == state) {
      //     _swipeController.stopAutoplay();
      //   } else {
      //     _swipeController.startAutoplay();
      //   }
      //   return coloredRefreshHeader(
      //       Color(parseInt(_currentCover?.color, defaultColor: Colors.grey)),
      //       text);
      // },
      header: (ctx) {
        if (newsBannerList.length > 0) {
          return Container(
            margin: const EdgeInsets.only(left: 15, top: 15, right: 15),
            child: ScrollerCover(
              newsBannerList.length,
              (ctx, index) {
                NewsCoverEntity each = newsBannerList[index];
                _currentCover = each;
                return GestureDetector(
                  child: NewsCover(each),
                  onTap: ClickUtils.debounce(
                      () => RouteManager.push(context, each.linkUrl)),
                );
              },
              height: 118,
              controller: _swipeController,
              // margin: const EdgeInsets.only(bottom: 15),
              indicatorBuilder: (index, count) => CircleIndicator(index, count),
            ),
          );
        } else {
          return Container();
        }
      },
      getData: (pageNumber) async {
        if (pageNumber == 1) {
          await fetchNewsBanner();
          setState(() {});
        }
        List<ArticleListItem> lists = await API.shared.article
            .articleList(pageNumber, alias: widget.entity.alias);
        return lists
            .map((e) => FlowItemEntity.create(
                e.title, e.publish_time, e.thumb, e.linkUrl,
                labelText: e.tag, showBig: e.showBig == "1"))
            .toList();
      },
    );
  }
}
